package org.example.votingsystem.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.votingsystem.account.Result;
import org.example.votingsystem.dao.YearPeopleDao;
import org.example.votingsystem.items.YearPeople;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/api/yearPeople")
public class YearPeopleServelet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        YearPeopleDao yearPeopleDao = new YearPeopleDao();
        try {
            ArrayList<YearPeople> arrayList = yearPeopleDao.searchYearPeople();
            Result result = new Result(200,"查找成功！",arrayList);
            resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
        } catch (SQLException e) {
            Result result = new Result(500,"查找成功！",null);
            resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
